package com.dudu.sms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.mob.MobSDK;
import com.mob.secverify.CustomUIRegister;
import com.mob.secverify.CustomViewClickListener;
import com.mob.secverify.GetTokenCallback;
import com.mob.secverify.OperationCallback;
import com.mob.secverify.PageCallback;
import com.mob.secverify.SecVerify;
import com.mob.secverify.VerifyCallback;
import com.mob.secverify.common.exception.VerifyException;
import com.mob.secverify.datatype.UiSettings;
import com.mob.secverify.datatype.VerifyResult;
import com.mob.tools.utils.ResHelper;

import java.util.ArrayList;
import java.util.List;

public class SecVerifyMainActivity extends Activity {
    public static String TAG = "SecVerifyActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sec_verify_main);

        findViewById(R.id.submitPolicyBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitPolicy();
            }
        });

        findViewById(R.id.preVerifyBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                preVerify();
            }
        });

        findViewById(R.id.verifyBtn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify1();
            }
        });

        findViewById(R.id.verifyBtn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify2();
            }
        });

        findViewById(R.id.adapterBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SecVerify.setAdapterClass(MyLoginAdapter.class);
            }
        });

        findViewById(R.id.uiSettingBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiSetting();
            }
        });
    }

    /**
     * 提交隐私
     */
    private void submitPolicy(){
        MobSDK.submitPolicyGrantResult(true, new com.mob.OperationCallback<Void>() {
            @Override
            public void onComplete(Void data) {
                Log.d(TAG, "隐私协议授权结果提交：成功");
                Toast.makeText(SecVerifyMainActivity.this, "隐私协议授权结果提交：成功", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d(TAG, "隐私协议授权结果提交：失败");
                Toast.makeText(SecVerifyMainActivity.this, "隐私协议授权结果提交：失败" + t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 预取号
     */
    private void preVerify(){
        SecVerify.setTimeOut(5000);//设置超时时间
        //建议提前调用预登录接口，可以加快免密登录过程，提高用户体验
        SecVerify.preVerify(new OperationCallback() {
            @Override
            public void onComplete(Object data) {
                //TODO处理成功的结果
                Log.d("SecVerifyActivity", "data:" + data);
                Toast.makeText(SecVerifyMainActivity.this, "预处理处理成功", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(VerifyException e) {
                //TODO处理失败的结果
                if (e != null) {
                    Toast.makeText(SecVerifyMainActivity.this, "预处理失败    错误码:" + e.getCode() + "   错误Message：" + e.getMessage() + "   错误getCause：" + (e.getCause() == null ? "无" : e.getCause().getMessage()), Toast.LENGTH_LONG).show();
                    Log.d("SecVerifyActivity", "VerifyException:  错误码:" + e.getCode() + "   错误Message：" + e.getMessage() + "   错误getCause：" + (e.getCause() == null ? "无" : e.getCause().getMessage()));
                }
            }
        });
    }

    private void verify1(){

        SecVerify.verify(new VerifyCallback() {
            @Override
            public void onOtherLogin() {
                // 用户点击“其他登录方式”，处理自己的逻辑
            }
            @Override
            public void onUserCanceled() {
                // 用户点击“关闭按钮”或“物理返回键”取消登录，处理自己的逻辑
            }
            @Override
            public void onComplete(VerifyResult verifyResult) {
                // 获取授权码成功，将token信息传给应用服务端，再由应用服务端进行登录验证，此功能需由开发者自行实现
                //这里使用data.getOpToken()把获取到的token搭配服务端API进行验证
                Toast.makeText(SecVerifyMainActivity.this, "获取授权码成功" + verifyResult.getOpToken() + "  " + verifyResult.getToken() + "  " + verifyResult.getOperator(), Toast.LENGTH_LONG).show();

                Log.d("SecVerifyActivity", verifyResult.getOpToken() + "  " + verifyResult.getToken() + "  " + verifyResult.getOperator());
            }
            @Override
            public void onFailure(VerifyException e) {
                //TODO处理失败的结果
                if (e != null) {
                    if (e.getCause() != null) {
                        Toast.makeText(SecVerifyMainActivity.this, "onFailure:" + e.getMessage() + e.getCause().getMessage(), Toast.LENGTH_LONG).show();
                        Log.d("SecVerifyActivity", "onFailure:" + e.getMessage() + e.getCause().getMessage());
                    } else {
                        Toast.makeText(SecVerifyMainActivity.this, "onFailure:" + e.getMessage(), Toast.LENGTH_LONG).show();
                        Log.d("SecVerifyActivity", "onFailure:" + e.getMessage());
                    }
                } else {
                    Toast.makeText(SecVerifyMainActivity.this, "onFailure:  e == null", Toast.LENGTH_LONG).show();
                    Log.d("SecVerifyActivity", "onFailure:  e == null");

                }
                SecVerify.finishOAuthPage();
            }
        });
    }


    private void verify2(){

        SecVerify.verify(new PageCallback() {
            @Override
            public void pageCallback(int code, String desc) {
                //授权页面相关回调及错误码，无法打开运营商的授权页面的错误会在这里回调
                Toast.makeText(SecVerifyMainActivity.this, "授权页面相关回调::code=:" + code + "::desc=" + desc, Toast.LENGTH_LONG).show();
                Log.d("SecVerifyActivity", "授权页面相关回调::code=:" + code + "::desc=" + desc);

            }
        }, new GetTokenCallback() {
            @Override
            public void onComplete(VerifyResult verifyResult) {
                Toast.makeText(SecVerifyMainActivity.this, "获取授权码成功" + verifyResult.getOpToken() + "  " + verifyResult.getToken() + "  " + verifyResult.getOperator(), Toast.LENGTH_LONG).show();

                Log.d("SecVerifyActivity", verifyResult.getOpToken() + "  " + verifyResult.getToken() + "  " + verifyResult.getOperator());
                // 获取授权码成功，将token信息传给应用服务端，再由应用服务端进行登录验证，此功能需由开发者自行实现
//                        SecVerify.finishOAuthPage();
            }

            @Override
            public void onFailure(VerifyException e) {
                // TODO处理失败的结果
                if (e != null) {
                    if (e.getCause() != null) {
                        Toast.makeText(SecVerifyMainActivity.this, "onFailure:" + e.getMessage() + e.getCause().getMessage(), Toast.LENGTH_LONG).show();
                        Log.d("SecVerifyActivity", "onFailure:" + e.getMessage() + e.getCause().getMessage());
                    } else {
                        Toast.makeText(SecVerifyMainActivity.this, "onFailure:" + e.getMessage(), Toast.LENGTH_LONG).show();
                        Log.d("SecVerifyActivity", "onFailure:" + e.getMessage());
                    }
                } else {
                    Toast.makeText(SecVerifyMainActivity.this, "onFailure:  e == null", Toast.LENGTH_LONG).show();
                    Log.d("SecVerifyActivity", "onFailure:  e == null");

                }
                SecVerify.finishOAuthPage();
            }
        });
    }



    private void uiSetting(){
        UiSettings uiSettings = new UiSettings.Builder()
                .setNavTransparent(true)//标题栏是否透明
//                        .setNavHidden(false)//标题栏是否隐藏
                .setImmersiveTheme(true)//状态栏是否透明 （5.0以上生效）
                .setBackgroundImgId(R.drawable.sec_back)//设置背景图片资源ID 例：R.drawable.background
                .setImmersiveStatusTextColorBlack(true)//状态栏文字颜色是否为黑色（6.0以上生效）
                .setCheckboxHidden(false)
                .setCheckboxDefaultState(true)
                //                        .setNumberColorId(R.color.sec_verify_text_color_common_white)//标题栏文字颜色资源ID例：R.color.sec_verify_demo_number_color
                        .setNavCloseImgHidden(false)//标题栏返回按钮是否隐藏
//                        .setNavColorId(R.color.tra)//标题栏背景色资源ID 例：R.color.sec_verify_nav_color
                        .setCheckboxDefaultState(true)//隐私协议复选框默认状态，默认为true
                        .setAgreementBaseTextColorId(R.color.teal_200)//隐私协议其他文字颜色资源ID 例：R.color.sec_verify_demo_agreement_base_text_color
                        .setSloganTextSize(30)//Slogan文字大小（单位：sp） 例：16

                        .setSloganHidden(true)
                        .setDialogTheme(true)//设置是否使用弹窗模式，默认false
                        .setDialogOffsetY(1000)
                        .setDialogAlignBottom(true)
                        .setLoginBtnTextId("一键登录")//登录按钮文字 例：“一键登录”
                        .setCusAgreementNameId1( "隐私服务协议一")
                        .setCusAgreementUrl1("http://www.mob.com")//自定义隐私协议一URL 例：http://www.mob.com
                        .setCusAgreementNameId2( "隐私服务协议二")
                        .setCusAgreementUrl2("http://baidu.com")
                        .setCusAgreementColor1(0xfffe7a4e)
                        .setCusAgreementColor2(0xfffe7a4e)
                .setTranslateAnim(true)
//                        .setImmersiveTheme(true)
                .build();

        SecVerify.setUiSettings(uiSettings);

                /**
                 * 添加自定义view
                 */
                ImageView iv1 = new ImageView(this);
                iv1.setId(R.id.customized_btn_id_1);
                iv1.setImageDrawable(getResources().getDrawable(R.drawable.test_icon));
                RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
                params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                params1.bottomMargin = ResHelper.dipToPx(this, 80);
                params1.width = ResHelper.dipToPx(this, 30);
                params1.height = ResHelper.dipToPx(this, 30);
                iv1.setLayoutParams(params1);

                List<View> views = new ArrayList<View>();
                views.add(iv1);

                CustomUIRegister.addCustomizedUi(views, new CustomViewClickListener() {
                    @Override
                    public void onClick(View view) {
                        int id = view.getId();
                        if (id == R.id.customized_btn_id_1) {
                            Toast.makeText(SecVerifyMainActivity.this,"CustomizedUi Click",Toast.LENGTH_LONG).show();
                        }
                    }
                });

    }

}